package ictbk.ttmk51.grapro.app.utils;

import ictbk.ttmk51.grapro.app.safesms.SMS;

import java.text.ParseException;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class SMSProvider {
	private ContentResolver	cr;

	public SMSProvider(ContentResolver cr, Cursor cur) {
		this.cr = cr;
	}

	public SMSProvider() {
		
	}

	public ContentResolver getCr() {
		return cr;
	}

	public void setCr(ContentResolver cr) {
		this.cr = cr;
	}
	
	
	public ArrayList<SMS> getAllSMSList(Context context){
		ArrayList<SMS> AllSMSList = new ArrayList<SMS>();
		Uri smsUri = Uri.parse("content://sms/");
		Log.i("smsUri", smsUri.toString());
		String [] projection = new String[]{
				"_id",
				"thread_id",
				"body",
				"address",
				"read",
				"type",
				"status",
				"date",
				"seen",
				"person",
				"protocol",
				"reply_path_present",
				"subject",
				"locked",
				"error_code",
				"service_center",
		};
		Cursor cur =  context.getContentResolver().query(smsUri, projection, null, null, "date DESC");//Sao lại lỗi nhỉ???
		
		while(cur.moveToNext()){
			SMS SMSObject = new SMS();
			long id = cur.getLong(cur.getColumnIndex("_id"));
			long thread_id = cur.getLong(cur.getColumnIndex("thread_id"));
			String address = cur.getString(cur.getColumnIndex("address"));
			String body = cur.getString(cur.getColumnIndex("body"));
			int read = cur.getInt(cur.getColumnIndex("read"));
			int type = cur.getInt(cur.getColumnIndex("type"));
			long status = cur.getLong(cur.getColumnIndex("status"));
			String seen = cur.getString(cur.getColumnIndex("seen"));
			long ldate = cur.getLong(cur.getColumnIndex("date"));
			String person = cur.getString(cur.getColumnIndex("person"));
			String protocol = cur.getString(cur.getColumnIndex("protocol"));
			String reply_path_present = cur.getString(cur.getColumnIndex("reply_path_present"));
			String subject = cur.getString(cur.getColumnIndex("subject"));
			String service_center = cur.getString(cur.getColumnIndex("service_center"));
			String locked = cur.getString(cur.getColumnIndex("locked"));
			String error_code = cur.getString(cur.getColumnIndex("error_code"));
			
			SMSObject.setID(id);
			SMSObject.setThread_ID(thread_id);
			SMSObject.setAddress(address);
			SMSObject.setBody(body);
			SMSObject.setRead(read);
			SMSObject.setStatus(status);
			SMSObject.setType(type);
			SMSObject.setDate(ldate);
			SMSObject.setSEEN(seen);
			SMSObject.setProtocol(protocol);
			SMSObject.setPerson(person);
			SMSObject.setReply_path_present(reply_path_present);
			SMSObject.setSubject(subject);
			SMSObject.setService_center(service_center);
			SMSObject.setLocked(locked);
			SMSObject.setError_code(error_code);
			
			AllSMSList.add(SMSObject);
			
		}
		cur.close();
		return AllSMSList;
		
	}
	
	//Phương thức getAllSMS khác
	public ArrayList<SMS> getAllSMS(){
		ArrayList<SMS> LstSMS = new ArrayList<SMS>();
		//Cach khac viet vao day nhe!
		return LstSMS;
	}
	
	public void WriteSMS(Context context, SMS SMSObject) throws ParseException{
		
		Uri smsUri = Uri.parse("content://sms/");
		ContentValues values = new ContentValues();
		values.put("body", SMSObject.getBody());
		values.put("address", SMSObject.getAddress());
		values.put("type", SMSObject.getType());
		values.put("read", SMSObject.getRead());
		values.put("status", SMSObject.getStatus());
		values.put("date", SMSObject.getDate());
		values.put("seen", SMSObject.getSEEN());
		values.put("person", SMSObject.getPerson());
		values.put("protocol", SMSObject.getProtocol());
		values.put("reply_path_present", SMSObject.getReply_path_present());
		values.put("service_center", SMSObject.getService_center());
		values.put("subject", SMSObject.getSubject());
		values.put("locked", SMSObject.getLocked());
		values.put("error_code", SMSObject.getError_code());
		context.getContentResolver().insert(smsUri, values);
		
	}
	
	public boolean writeSMS(Context context, ArrayList<SMS> SMSList){
		try{
			for(SMS smsObject : SMSList){
				WriteSMS(context, smsObject);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}
